package com.itboye.sunsun.personsetting.ui;

import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;
import android.widget.RelativeLayout;
import android.widget.Toast;

import java.util.Set;

import com.itboye.sunsun.MainActivity;
import com.itboye.sunsun.R;
import com.itboye.sunsun.application.App;
import com.itboye.sunsun.beans.PersonDataBean;
import com.itboye.sunsun.constants.SPContants;
import com.itboye.sunsun.login.ui.LoginActivity;
import com.itboye.sunsun.network.HttpRequest;
import com.itboye.sunsun.person.ui.PersonSettingActivity;
import com.itboye.sunsun.support.BaseActivity;
import com.itboye.sunsun.utils.DebugLog;
import com.itboye.sunsun.utils.SPUtils;
import com.itboye.sunsun.volley.CodeErrorException;
import com.itboye.sunsun.volley.MyJsonRequest;
import com.itboye.sunsun.volley.XErrorListener;
import com.itboye.sunsun.volley.XRequestListener;

public class SetNickNameActivity extends BaseActivity implements OnClickListener {

	private Button confirm;
	private EditText edittext;
	private RelativeLayout backrela;

	@Override
	protected void onCreate(Bundle arg0) {
		// TODO Auto-generated method stub
		super.onCreate(arg0);
		setContentView(R.layout.activity_nickname);
		setStatusBarColor(R.color.top_blue);
	}

	@Override
	public void onClick(View v) {
		if (v.getId() == R.id.confirm) {
			if (!edittext.getText().toString().equals(null)) {
				setNickName();
			}else
			{
				Toast.makeText(App.ctx, "输入错误", 0).show();
			}

		}else if(v.getId()==R.id.backrela)
		{
			this.finish();
		}

	}

	public void setNickName() {
		MyJsonRequest<String> request = new MyJsonRequest.Builder<String>()
				.apiVer("100")
				.typeKey("BY_User_update")
				.param("uid", (String) SPUtils.get(App.ctx, null, SPContants.USER_ID, ""))
				.param("nickname", edittext.getText().toString())
				.requestListener(new XRequestListener<String>() {

					@Override
					public void onResponse(String response) {
						// TODO Auto-generated method stub
						DebugLog.v("result", response.toString());
						//Toast.makeText(App.ctx, "修改成功", 0).show();
						SPUtils.put(App.ctx, null, SPContants.NICK, edittext.getText().toString());
						
						
						Intent intent=new Intent(SetNickNameActivity.this,PersonSettingActivity.class);
						intent.putExtra("nickname", edittext.getText().toString());
						setResult(RESULT_OK,intent);
						SetNickNameActivity.this.finish();
					}
				}).errorListener(new XErrorListener() {

					@Override
					public void onErrorResponse(Exception exception, int code, String msg) {
						if (exception instanceof CodeErrorException) {
							Toast.makeText(App.ctx, "网络异常", 0).show();
						} else {
							Toast.makeText(App.ctx, "网络异常", 0).show();
						}

					}
				}).build();
		HttpRequest.getDefaultRequestQueue().add(request);

	}

}
